//
//  FileUtill.m
//  Platform
//
//  Created by 张宁宁 on 16/9/22.
//  Copyright © 2016年 lirenkj. All rights reserved.
//

#import "FileUtil.h"
#import "StringUtil.h"

@implementation FileUtil

+(void)makeDirs:(NSString *)path
{
    int i = [StringUtil lastIndexOf:path searchStr:@"/"];
    int j = [StringUtil lastIndexOf:path searchStr:@"."];
    NSString *tmp;
    if(i<j){
        tmp = [StringUtil subString:path startIndex:0 endIndex:i];
    }else{
        tmp = path;
    }
    [self createDirectory:tmp withError:nil];
}

+(BOOL)createDirectory:(NSString *)filePath withError:(NSError*)error
{
    NSFileManager *fm = [NSFileManager defaultManager];
    return [fm createDirectoryAtPath:filePath
         withIntermediateDirectories:YES
                          attributes:nil
                               error:&error];
}

+ (BOOL)isExsit:(NSString *)filePath isDirectory:(BOOL)isDirectory
{
    if([StringUtil isEmpty:filePath needTrim:YES]){
        return NO;
    }
    NSFileManager *fm = [NSFileManager defaultManager];
    BOOL flag = [fm fileExistsAtPath:filePath isDirectory:&isDirectory];
    if(flag){
        return YES;
    }else{
        return NO;
    }
}

+ (BOOL)writeFile:(NSString *)path data:(NSData *)data
{
    [self makeDirs:path];
    return [data writeToFile:path atomically:YES];
}

+(BOOL)appendFileWithPath:(NSString*)path data:(NSData*)data{
    @try {
        if (![self isExsit:path isDirectory:NO]) {
            [self writeFile:path data:data];
            return YES;
        }else{
            NSFileHandle  *outFile = [NSFileHandle fileHandleForWritingAtPath:path];
            if(outFile == nil)
            {
                NSLog(@"Open of file for writing failed");
            }
            //找到并定位到outFile的末尾位置(在此后追加文件)
            [outFile seekToEndOfFile];
            [outFile writeData:data];
            //关闭读写文件
            [outFile closeFile];
            return YES;
        }
    }
    @catch (NSException *exception) {
        return NO;
    }
}

+ (BOOL)cleanDirectory:(NSString *)dirPath
{
    NSArray *files = [self listFilesFromPath:dirPath];
    if(files != nil&&[files count]>0){
        for (NSString *filePath in files) {
            [self removeFile:[dirPath stringByAppendingPathComponent:filePath]];
        }
    }
    return YES;
}

+ (NSArray *)listFilesFromPath:(NSString *)filePath
{
    NSFileManager *fm = [NSFileManager defaultManager];
    return [fm subpathsAtPath:filePath];
}

+ (BOOL)removeFile:(NSString *)filePath
{
    NSFileManager *fm = [NSFileManager defaultManager];
    NSError *error = nil;
    BOOL flag = NO;
    flag = [fm removeItemAtPath:filePath error:&error];
    return flag;
}

+ (NSData *)readFileFromLocalPath:(NSString *)filePath
{
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    return data;
}

+ (NSData *)readFileFromUrl:(NSString *)url
{
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
    return data;
}

+ (NSDictionary *)getFileAttribute:(NSString *)filePath
{
    NSFileManager *fm = [NSFileManager defaultManager];
    NSError *error;
    NSDictionary *dic = [fm attributesOfItemAtPath:filePath error:&error];
    return dic;
}

+ (BOOL)rename:(NSString *)sourceFilePath tagertFilePath:(NSString *)tagertFilePath
{
    [self makeDirs:tagertFilePath];
    NSFileManager *fm = [NSFileManager defaultManager];
    NSError *error;
    BOOL isSuccess  = [fm moveItemAtPath:sourceFilePath toPath:tagertFilePath error:&error];
    return isSuccess;
}

+ (BOOL)copy:(NSString *)sourceFilePath tagertFilePath:(NSString *)tagertFilePath
{
    [self makeDirs:tagertFilePath];
    NSFileManager *fm = [NSFileManager defaultManager];
    NSError *error;
    BOOL isSuccess = [fm copyItemAtPath:sourceFilePath toPath:tagertFilePath error:&error];
    return isSuccess;
}

@end
